class JobsitesController < ApplicationController
  skip_before_filter :require_login, :only => [:get_sites]
  
  def create
    @jobsite = Jobsite.new(:name => params[:name],
                              :company_id => params[:company])
    if @jobsite.save
      rtn_json({'status' => '200'})
    else
      rtn_json({'error' => 'Could not create jobsite'})
    end
  end
  
  def get_sites
    result = {}
    
    if params[:company]
      @jobsites = Jobsite.where(:company_id => params[:company])
    else
      @jobsites = Jobsite.all
    end
    
    if @jobsites
      rtn_json(@jobsites)
    else
      rtn_json({'error' => 'Error. Could not get all jobsites'})
    end
  end

  def rtn_json(obj)
    respond_to do |format|
      format.html{render json: obj, :callback => params[:callback]}
      format.json{render json: obj, :callback => params[:callback]}
    end
  end
end
